//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <iostream>
#include <fstream>

#include <curl/curl.h>

#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHINLINE    10000
#define DELAYTIMESECOND 120
#define DELAYBETWEENQ   15
#define DELAYPOSITION   10000
#define URLSUPERPOSE    "http://wishart.biology.ualberta.ca/cgi-bin/SuperPose/superpose.cgi?scenario=resolve_options&pdbid1=1eg2_A&pdbid2=1kim_A"
#define URLPDBJFATCAT   "http://www.rcsb.org/pdb/workbench/showPrecalcAlignment.do?action=pw_fatcat&pdb1=%s&chain1=%s&pdb2=%s&chain2=%s"
#define URLEXAMPLE1     "http://www.rcsb.org/pdb/workbench/showPrecalcAlignment.do?action=pw_fatcat&pdb1=1A52&chain1=A&pdb2=1KIM&chain2=A"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
static char   errorBuffer[CURL_ERROR_SIZE];
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
static int writer(char *data, size_t size, size_t nmemb, string *buffer) {
  int result = 0;
  if (buffer != NULL) {
    buffer->append(data, size * nmemb);
    result = size * nmemb;
  }
  return result;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
string EraseHTMLTAG(string text) {

  size_t  foundB, foundE;

  do {
    foundB = text.find("<");             if (foundB == string::npos) break;
    foundE = text.find(">", foundB + 1); if (foundE == string::npos) break;
    text.erase(int(foundB), int(foundE));
  } while (true);

  return text;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
char* SplitColumn(char* buffer, int position, const char* characters) {

  if (!buffer || !characters || (position <= 0)) return NULL;

  char *part = strtok(buffer, characters);
  if (position == 1) return part;

  int tmpPosition = 1;
  do {
    part = strtok(NULL, characters);
    tmpPosition++;
  } while (part && (tmpPosition != position));

  return part;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void ShowTime(time_t initialTime) {
    
    time_t endTime;    
    time(&endTime);
     
    int timeSeconds = (int)difftime(endTime, initialTime);
    int timeMinutes = timeSeconds/60;    
    int timeHours   = timeMinutes/60;    
    int timeDays    = timeHours/24;    
    
    timeSeconds    %= 60;    
    timeMinutes    %= 60;    
    timeHours      %= 24;   
    
    printf("Elapsed Time: %02d:%02d:%02d:%02d (DD:HH:MM:SS) |", timeDays, timeHours, timeMinutes, timeSeconds);    
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
char* ShowTime(time_t initialTime, char* buffer) {
    
    time_t endTime;    
    time(&endTime);
     
    int timeSeconds = (int)difftime(endTime, initialTime);
    int timeMinutes = timeSeconds/60;    
    int timeHours   = timeMinutes/60;    
    int timeDays    = timeHours/24;    
    
    timeSeconds    %= 60;    
    timeMinutes    %= 60;    
    timeHours      %= 24;   
    
    sprintf(buffer, "Elapsed Time: %02d:%02d:%02d:%02d (DD:HH:MM:SS) |\0", timeDays, timeHours, timeMinutes, timeSeconds);    
    
    return buffer;
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void Wait(int seconds) {
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main (int argc, char** argv) {

  char*  namefileIn  = NULL;
  char*  namefileOut = NULL;
  
  time_t initialTime;
  
  initialTime = time(NULL);

  if (argc < 3) {
    cout << "Error in the input parameters! Must by like: " << endl;
    cout << "./PDBjFATCATRigidCompute <Input File with one column with the following format \"pdbid,chain\"> <Output Similarity Matrix Name>" << endl;
    return 0;
  }
  else {
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
    cout << "  Input File : " << argv[1] << endl;
    cout << " Output File : " << argv[2] << endl;
    
    namefileIn  = new char[strlen(argv[1]) + 10];
    namefileOut = new char[strlen(argv[2]) + 10];
    strcpy(namefileIn,  argv[1]);
    strcpy(namefileOut, argv[2]);
  }

  fstream fileIn, fileOutPValue, fileOutScore, fileOutRMSD, fileOutIdentity, fileOutSimilarity, fileLog;
  char    buffer[LENGTHINLINE + 1];

  fileIn.open(namefileIn, fstream::in);

  if (!fileIn.is_open()) {
    if (namefileIn)  delete[] namefileIn;
    if (namefileOut) delete[] namefileOut;
    return 0;
  }
  
  TListE <String> *listFiles = new TListE <String> ();
  
  while (!fileIn.eof()) {
    fileIn.getline(buffer, LENGTHINLINE);
    String* nameFile = new String(buffer);
    nameFile->Trim();
    if (nameFile->Length()) listFiles->Add(nameFile);
  }
  fileIn.close();
  
  char* completeName = new char[strlen(namefileOut) + 30];
  
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
  sprintf(completeName, "%s_PValue.dat\0", namefileOut);
  fileOutPValue.open(completeName, fstream::out);
  cout << "Probability Value (P-Value) File :\n " << completeName << endl;
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  sprintf(completeName, "%s_Score.dat\0", namefileOut);
  fileOutScore.open(completeName, fstream::out);
  cout << "Score's Alignment File :\n " << completeName << endl;
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  sprintf(completeName, "%s_RMSD.dat\0", namefileOut);
  fileOutRMSD.open(completeName, fstream::out);
  cout << "Output Root-mean-square Deviation of superposition (Ångströms) File :\n " << completeName << endl;
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  sprintf(completeName, "%s_Identity.dat\0", namefileOut);
  fileOutIdentity.open(completeName, fstream::out);
  cout << "Output Sequence identity of query and target in the equivalent regions File :\n " << completeName << endl;
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  sprintf(completeName, "%s_Similarity.dat\0", namefileOut);
  fileOutSimilarity.open(completeName, fstream::out);
  cout << "Similarity Value File (the matrix isn't simetric) :\n " << completeName << endl;
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  sprintf(completeName, "%s.log\0", namefileOut);
  fileLog.open(completeName, fstream::out);
  cout << "Log File :\n " << completeName << endl;
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
  
  if (completeName) delete[] completeName;

  if (!fileOutPValue.is_open()   || !fileOutScore.is_open() || !fileOutRMSD.is_open() ||
      !fileOutIdentity.is_open() || !fileOutSimilarity.is_open()) {
    if (namefileIn)  delete[] namefileIn;
    if (namefileOut) delete[] namefileOut;
    if (listFiles)   delete   listFiles;
    return 0;
  }


  cout << "P-Value" << endl;
  cout << "  P-Value is the Probalility value that the two structure are similar" << endl;
  cout << "  According the FATCAT algorithm, a pair with value < 0.05 is significantly similar." << endl;
  cout << "Score" << endl;
  cout << "  The raw score of the Alignment." << endl;
  cout << "RMSD" << endl;
  cout << "  Root-mean-square Deviation of superposition (Ångströms)." << endl;
  cout << "Identity" << endl;
  cout << "  The percent of identity in the Alignment." << endl;
  cout << "  (= 100 x S/Lt)" << endl;
  cout << "Similarity" << endl;
  cout << "  The similarity relative to each molecule" << endl;
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  ShowTime(initialTime);
  cout.width(14); cout.left; cout << "        Target |";
  cout.width(14); cout.left; cout << "         Query |";
  cout.width(12); cout.left; cout << "     P-Value |";
  cout.width(8);  cout.left; cout << "   Score |";
  cout.width(8);  cout.left; cout << "    RMSD |";
  cout.width(10); cout.left; cout << "  Identity |";
  cout.width(13); cout.left; cout << " Similarity T |";
  cout.width(13); cout.left; cout << " Similarity Q |";
  cout << endl;
  cout << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  fileLog << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
  fileLog << "Console PDB FATCAT Rigid Result" << endl;
  fileLog << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
  fileLog << "Parameters:" << endl;
  fileLog << "P-Value" << endl;
  fileLog << "  P-Value is the Probalility value that the two structure are similar" << endl;
  fileLog << "  According the FATCAT algorithm, a pair with value < 0.05 is significantly similar." << endl;
  fileLog << "Score" << endl;
  fileLog << "  The raw score of the Alignment." << endl;
  fileLog << "RMSD" << endl;
  fileLog << "  Root-mean-square Deviation of superposition (Ångströms)." << endl;
  fileLog << "Identity" << endl;
  fileLog << "  The percent of identity in the Alignment." << endl;
  fileLog << "  (= 100 x S/Lt)" << endl;
  fileLog << "Similarity" << endl;
  fileLog << "  The similarity relative to each molecule" << endl;
  fileLog << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  fileLog << ShowTime(initialTime, buffer);
  fileLog.width(14); fileLog.left; fileLog << "        Target |";
  fileLog.width(14); fileLog.left; fileLog << "         Query |";
  fileLog.width(12); fileLog.left; fileLog << "     P-Value |";
  fileLog.width(8);  fileLog.left; fileLog << "   Score |";
  fileLog.width(8);  fileLog.left; fileLog << "    RMSD |";
  fileLog.width(10); fileLog.left; fileLog << "  Identity |";
  fileLog.width(13); fileLog.left; fileLog << " Similarity T |";
  fileLog.width(13); fileLog.left; fileLog << " Similarity Q |";
  fileLog << endl;
  fileLog << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  double** matrixPValue     = new double*[listFiles->Length()];
  double** matrixScore      = new double*[listFiles->Length()];
  double** matrixRMSD       = new double*[listFiles->Length()];
  double** matrixIdentity   = new double*[listFiles->Length()];
  double** matrixSimilarity = new double*[listFiles->Length()];
  
  for (int i = 0; i < listFiles->Length(); i++) {
      
    matrixPValue[i]     = new double[listFiles->Length()];
    matrixScore[i]      = new double[listFiles->Length()];
    matrixRMSD[i]       = new double[listFiles->Length()];
    matrixIdentity[i]   = new double[listFiles->Length()];
    matrixSimilarity[i] = new double[listFiles->Length()];
    
    fileOutPValue.width(12);     fileOutPValue.left;     fileOutPValue     << listFiles->Get(i)->Out();
    fileOutScore.width(12);      fileOutScore.left;      fileOutScore      << listFiles->Get(i)->Out();
    fileOutRMSD.width(10);       fileOutRMSD.left;       fileOutRMSD       << listFiles->Get(i)->Out();
    fileOutIdentity.width(10);   fileOutIdentity.left;   fileOutIdentity   << listFiles->Get(i)->Out();
    fileOutSimilarity.width(10); fileOutSimilarity.left; fileOutSimilarity << listFiles->Get(i)->Out();
      
  }
  
  fileOutPValue     << endl;
  fileOutScore      << endl;
  fileOutRMSD       << endl;
  fileOutIdentity   << endl;
  fileOutSimilarity << endl;
    
  curl_global_init(CURL_GLOBAL_ALL);

  for (int i = 0, z = 0; i < listFiles->Length(); i++) {

    for (int j = i; j < listFiles->Length(); j++, z++) {
        
      if (i == j) {
        
        matrixPValue[i][j]     = 0;
        matrixScore[i][j]      = 1000;
        matrixRMSD[i][j]       = 0;
        matrixIdentity[i][j]   = 100;
        matrixSimilarity[i][j] = 100;
          
        continue;
        
      }

      if (listFiles->Get(i)->Length() < 6 ) break;
      if (listFiles->Get(j)->Length() < 6 ) continue;
      
      char      url[5000], pdbchain1[5], pdbchain2[5], chain1[2], chain2[2];

      CURL*     curl;
      CURLcode  result;

      string    bufferURL;

      strncpy(pdbchain1, listFiles->Get(i)->Out(), 4);
      strncpy(pdbchain2, listFiles->Get(j)->Out(), 4);

      strncpy(chain1, listFiles->Get(i)->Out() + 5, 1);
      strncpy(chain2, listFiles->Get(j)->Out() + 5, 1);

      pdbchain1[4] = '\0';
      pdbchain2[4] = '\0';
      chain1[1]    = '\0';
      chain2[1]    = '\0';
      
      sprintf(url, URLPDBJFATCAT, pdbchain1, chain1, pdbchain2, chain2);
      //cout << "Retrieving " << url << endl;

      curl = curl_easy_init();

      if (curl) {

        curl_easy_setopt(curl, CURLOPT_ERRORBUFFER,     errorBuffer);
        curl_easy_setopt(curl, CURLOPT_URL,             url);
        curl_easy_setopt(curl, CURLOPT_HEADER,          1);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,  1);
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL,        1);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,   writer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA,       &bufferURL);
        //curl_easy_setopt(curl, CURLOPT_VERBOSE,         1L);
        curl_easy_setopt(curl, CURLOPT_USERAGENT,       "libcurl-agent/1.0");


        result = curl_easy_perform(curl);
        //Wait(DELAYBETWEENQ);

        if (result == CURLE_OK) {

          string  str, strX, strY, strZ;
          size_t  foundNotYet, foundNotYetX, foundNotYetY, foundNotYetZ;

          bool    condition = false;

          str  = "are not ready yet.";
          strX = "We're sorry, but a portion of the page";
          strY = "probably due to heavy traffic on the structure alignment server";
          strZ = "Error report";

          foundNotYet  = bufferURL.find(str);
          foundNotYetX = bufferURL.find(strX);
          foundNotYetY = bufferURL.find(strY);
          foundNotYetZ = bufferURL.find(strZ);
          
          while (foundNotYet != string::npos || foundNotYetX != string::npos || foundNotYetY != string::npos || foundNotYetZ != string::npos) {

            //Wait(DELAYTIMESECOND);
            bufferURL.clear();

            curl_easy_cleanup(curl);
            curl = curl_easy_init();
            
            curl_easy_setopt(curl, CURLOPT_ERRORBUFFER,     errorBuffer);
            curl_easy_setopt(curl, CURLOPT_URL,             url);
            curl_easy_setopt(curl, CURLOPT_HEADER,          1);
            curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,  1);
            curl_easy_setopt(curl, CURLOPT_NOSIGNAL,        1);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,   writer);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA,       &bufferURL);

            result = curl_easy_perform(curl);
            //Wait(DELAYTIMESECOND);
            
            if (result != CURLE_OK) { condition = true; break; }
            foundNotYet  = bufferURL.find(str);
            foundNotYetX = bufferURL.find(strX);
            foundNotYetY = bufferURL.find(strY);
            foundNotYetZ = bufferURL.find(strZ);

          }

          curl_easy_cleanup(curl);

          if (condition) {
            j--; z--; continue;
          }

          size_t  foundPValueI,           foundPValueF;
          size_t  foundScoreI,            foundScoreF;
          size_t  foundRMSDI,             foundRMSDF;
          size_t  foundIdentityI,         foundIdentityF;
          size_t  foundSimilarityTargetI, foundSimilarityTargetF;
          size_t  foundSimilarityQueryI,  foundSimilarityQueryF;

          //P-value read (Structural Alignment)
          str = "P-value:</a>";         foundPValueI           = bufferURL.find(str);
          str = "<br />";               foundPValueF           = bufferURL.find(str, foundPValueI + 1);
          string pValue = bufferURL.substr(foundPValueI + strlen("P-value:</a>"), foundPValueF - foundPValueI - strlen("P-value:</a>"));

          //Score read (Structural Alignment)
          str = "Score:</a>";           foundScoreI            = bufferURL.find(str, foundPValueF + 1);
          str = "<br />";               foundScoreF            = bufferURL.find(str, foundScoreI  + 1);
          string score = bufferURL.substr(foundScoreI + strlen("Score:</a>"), foundScoreF - foundScoreI - strlen("Score:</a>"));

          //RMSD read (Structural Alignment)
          str = "RMSD:</a>";            foundRMSDI             = bufferURL.find(str, foundScoreF + 1);
          str = "<br />";               foundRMSDF             = bufferURL.find(str, foundRMSDI  + 1);
          string rmsd = bufferURL.substr(foundRMSDI + strlen("RMSD:</a>"), foundRMSDF - foundRMSDI - strlen("RMSD:</a>"));

          //Identity read (Structural Alignment)
          str = "%Id:</a>";             foundIdentityI         = bufferURL.find(str, foundRMSDF     + 1);
          str = "%<br />";              foundIdentityF         = bufferURL.find(str, foundIdentityI + 1);
          string identity = bufferURL.substr(foundIdentityI + strlen("%Id:</a>"), foundIdentityF - foundIdentityI - strlen("%Id:</a>"));
          
          //Similarity Target (MOL1) read (Structural Alignment)
          str = "Similarity:</a></td>"; foundSimilarityTargetI = bufferURL.find(str, foundIdentityF         + 1);
          str = "<td>";                 foundSimilarityTargetI = bufferURL.find(str, foundSimilarityTargetI + 1);
          str = "%</td>";               foundSimilarityTargetF = bufferURL.find(str, foundSimilarityTargetI + 1);
          string similarityT = bufferURL.substr(foundSimilarityTargetI + strlen("<td>"), foundSimilarityTargetF - foundSimilarityTargetI - strlen("<td>"));

          //Similarity Query (MOL2) read (Structural Alignment)
          str = "Similarity:</a></td>"; foundSimilarityQueryI  = bufferURL.find(str, foundSimilarityTargetF + 1);
          str = "<td>";                 foundSimilarityQueryI  = bufferURL.find(str, foundSimilarityQueryI  + 1);
          str = "%</td>";               foundSimilarityQueryF  = bufferURL.find(str, foundSimilarityQueryI  + 1);
          string similarityQ = bufferURL.substr(foundSimilarityQueryI + strlen("<td>"), foundSimilarityQueryF - foundSimilarityQueryI - strlen("<td>"));

          matrixPValue[i][j]     = matrixPValue[j][i]   = atof(pValue.data());
          matrixScore[i][j]      = matrixScore[j][i]    = atof(score.data());
          matrixRMSD[i][j]       = matrixRMSD[j][i]     = atof(rmsd.data());
          matrixIdentity[i][j]   = matrixIdentity[j][i] = atoi(identity.data());
          matrixSimilarity[i][j] = atof(similarityT.data());
          matrixSimilarity[j][i] = atof(similarityQ.data());

          ShowTime(initialTime);
          cout.width(14); cout.left; cout << listFiles->Get(i)->Out() << " |";
          cout.width(14); cout.left; cout << listFiles->Get(j)->Out() << " |";
          cout.width(12); cout.left; cout << pValue                   << " |";
          cout.width(8);  cout.left; cout << score                    << " |";
          cout.width(8);  cout.left; cout << rmsd                     << " |";
          cout.width(10); cout.left; cout << identity                 << " |";
          cout.width(13); cout.left; cout << similarityT              << " |";
          cout.width(13); cout.left; cout << similarityQ              << " |";
          cout << endl;

          fileLog << ShowTime(initialTime, buffer);
          fileLog.width(14); fileLog.left; fileLog << listFiles->Get(i)->Out() << " |";
          fileLog.width(14); fileLog.left; fileLog << listFiles->Get(j)->Out() << " |";
          fileLog.width(12); fileLog.left; fileLog << pValue                   << " |";
          fileLog.width(8);  fileLog.left; fileLog << score                    << " |";
          fileLog.width(8);  fileLog.left; fileLog << rmsd                     << " |";
          fileLog.width(10); fileLog.left; fileLog << identity                 << " |";
          fileLog.width(13); fileLog.left; fileLog << similarityT              << " |";
          fileLog.width(13); fileLog.left; fileLog << similarityQ              << " |";
          fileLog << endl;

        }
        else curl_easy_cleanup(curl);

      }
      
    }
    
  }

  curl_global_cleanup();
  
  fileLog << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
  cout    << "-----------------------------------------------------------------------------------------------------------------------------------------------------" << endl;

  for (int i = 0; i < listFiles->Length(); i++) {

    for (int j = 0; j  < listFiles->Length(); j++) {

      fileOutPValue.width(12);     fileOutPValue.left;     fileOutPValue     << matrixPValue[i][j];
      fileOutScore.width(12);      fileOutScore.left;      fileOutScore      << matrixScore[i][j];
      fileOutRMSD.width(10);       fileOutRMSD.left;       fileOutRMSD       << matrixRMSD[i][j];
      fileOutIdentity.width(10);   fileOutIdentity.left;   fileOutIdentity   << matrixIdentity[i][j];
      fileOutSimilarity.width(10); fileOutSimilarity.left; fileOutSimilarity << matrixSimilarity[i][j];
      
    }
    
    fileOutPValue     << endl;
    fileOutScore      << endl;
    fileOutRMSD       << endl;
    fileOutIdentity   << endl;
    fileOutSimilarity << endl;
    
    if (matrixPValue[i])     delete[] matrixPValue[i];
    if (matrixScore[i])      delete[] matrixScore[i];
    if (matrixRMSD[i])       delete[] matrixRMSD[i];
    if (matrixIdentity[i])   delete[] matrixIdentity[i];
    if (matrixSimilarity[i]) delete[] matrixSimilarity[i];
    
  }

  if (matrixPValue)     delete[] matrixPValue;
  if (matrixScore)      delete[] matrixScore;
  if (matrixRMSD)       delete[] matrixRMSD;
  if (matrixIdentity)   delete[] matrixIdentity;
  if (matrixSimilarity) delete[] matrixSimilarity;

  fileOutPValue.close();
  fileOutScore.close();
  fileOutRMSD.close();
  fileOutIdentity.close();
  fileOutSimilarity.close();
  fileLog.close();

  if (namefileIn)  delete[] namefileIn;
  if (namefileOut) delete[] namefileOut;
  if (listFiles)   delete   listFiles;

  return 1;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//


